package com.orange.base.core.config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.core.config.Order;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Order(100)
@Component
public class AfterServiceStarted implements ApplicationRunner {

    private static Log LOGGER = LogFactory.getLog(AfterServiceStarted.class);

    /**
     * 会在服务启动完成后立即执行
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        char[] chars = "Orange initialization success ^_^".toCharArray();
        printInitMsg(chars);
    }

    private void printInitMsg(char[] chars) {
        int wordLength = 0;
        int touHigh = 6;
        int jianHigh = 25;
        int kuang = 50;
        StringBuilder sb = new StringBuilder("\n");
        for (int i = 1; i < kuang; i++) {
            sb.append("-");
        }
        for (int i = 1; i <= touHigh + jianHigh; i++) {
            if (i > 23) {
                break;
            }
            sb.append("\n");
            for (int j = 1; j <= kuang; j++) {
                // 上三角
                if (i <= touHigh) {
                    if (j >= (kuang / 2 + 1) + 1 - i && j <= (kuang / 2 + 1) - 1 + i) {
                        sb.append("*");
                    } else {
                        sb.append("-");
                    }
                }
                // 上三角一下部分
                if (i > touHigh && i <= jianHigh) {
                    if (j >= (kuang / 2 + 1) + 1 - i && j <= kuang - 3 * (i - touHigh)) {
                        sb.append("*");
                    } else if (j <= (kuang / 2 + 1) - 1 + i && j >= 0 + 3 * (i - touHigh)) {
                        sb.append("*");
                    } else {
                        if (i == 21 && j > 9 && wordLength < chars.length) {
                            sb.append(chars[wordLength]);
                            wordLength++;
                        } else {
                            sb.append("-");
                        }

                    }
                }
            }
        }
        LOGGER.info(sb.toString());
    }
}